<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button class="post-btn" @click="addComment" :disabled="!message.length">发布</van-button>
  </div>
</template>

<script>
import { issueComment } from '@/api/comment'
export default {
  name: 'commentIssue',
  components: {},
  props: {
    artId: {
      type: [Number, String, Object],
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      default: null
    }
  },
  // inject: {
  //   articleId: {
  //     type: [Number, String, Object],
  //     default: null
  //   }
  // },
  data() {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async addComment() {
      // 加载提示
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        // 设置转动时间，为0就无限转动
        duration: 0
      })
      try {
        const { data } = await issueComment({
          target: this.artId.toString(),
          content: this.message,
          art_id: this.articleId ? this.articleId.toString() : this.articleId
        })
        this.message = ''
        this.$emit('addComment', data.data)
        this.$toast.success('发布评论成功')
      } catch (err) {
        this.$toast.fail('发布评论失败，请重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
